
#include "socket_server.h"

void main()
{
    socket_client();
}

void socket_client()
{
    printf("socket client\n");
     char host[] = "127.0.0.1";
     int port = 12701;

    //char host[] = "192.168.10.143";
    //int port = 9999;


    // 通讯
    int fd_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (fd_socket == -1)
    {
        perror("socket error");
        exit(-1);
    }
    // 地址
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(host);
    server_addr.sin_port = htons(port);

    // 连接
    int fd_connect = connect(fd_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (fd_connect == -1)
    {
        perror("connect error");
        exit(-1);
    }
    printf("conn:%s\n", host);


    // 发送
    char buf[] = "over";
    printf("send:%s\n", buf);
    int fd_send = send(fd_socket, buf, sizeof(buf), 0);//默认传0
    if (fd_send == -1)
    {
        perror("send error");
        exit(-1);
    }
    // 关闭
    close(fd_socket);

    printf("main exit\n");
    exit(0);
}